package cn.exrick.xboot.base.serviceimpl

import cn.exrick.xboot.base.entity.EmployeeOfficialExamRecord
import cn.exrick.xboot.base.entity.EnterpriseEmployee
import cn.exrick.xboot.base.entity.ExamResultConfirmCheck
import cn.exrick.xboot.base.mapper.EmployeeOfficialExamRecordMapper
import cn.exrick.xboot.base.service.*
import cn.exrick.xboot.base.vo.EmployeeOfficialExamVo
import cn.exrick.xboot.core.common.utils.SecurityUtil
import cn.exrick.xboot.core.common.utils.ToolUtil
import cn.exrick.xboot.core.service.DepartmentService
import cn.exrick.xboot.core.service.UserService
import com.baomidou.mybatisplus.extension.kotlin.KtQueryWrapper
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl
import lombok.extern.slf4j.Slf4j
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.stereotype.Service
import org.springframework.transaction.annotation.Transactional

/**
 * 员工正式考试信息记录表接口实现
 * @author Fei
 */
@Slf4j
@Service
@Transactional
open class IEmployeeOfficialExamRecordServiceImpl : ServiceImpl<EmployeeOfficialExamRecordMapper?, EmployeeOfficialExamRecord?>(), IEmployeeOfficialExamRecordService {


    @Autowired
    private lateinit var iEnterpriseEmployeeService: IEnterpriseEmployeeService

    @Autowired
    private lateinit var iEnterpriseUserService: IEnterpriseUserService

    @Autowired
    private lateinit var iJobService: IJobService

    @Autowired
    private lateinit var iExamRoomService: IExamRoomService

    @Autowired
    private lateinit var userService: UserService

    @Autowired
    private lateinit var departmentService: DepartmentService

    @Autowired
    private lateinit var iSecurityAdvisorJobTrainPartService: ISecurityAdvisorJobTrainPartService

    @Autowired
    private lateinit var iExamResultConfirmCheckService: IExamResultConfirmCheckService

    @Autowired
    private lateinit var securityUtil: SecurityUtil

    override fun getEmployeeOfficialExamVo(id: String): EmployeeOfficialExamVo? {

        return getById(id)?.run {

            EmployeeOfficialExamVo().apply {

                ToolUtil.copyProperties(this@run, this)
                iEnterpriseEmployeeService.getOne(KtQueryWrapper(EnterpriseEmployee::class.java)
                        .eq(EnterpriseEmployee::userId, createBy))?.let {

                    enterpriseName = iEnterpriseUserService.getEnterpriseNameById(it.enterpriseId)

                    userService.get(employeeUserId)?.let {

                        employeeName = it.nickname
                        departmentTitle = it.departmentId?.let { departmentService.getDepartmentTitle(it) }
                        mobile = it.mobile

                        iEnterpriseEmployeeService.getOne(KtQueryWrapper(EnterpriseEmployee::class.java)
                                .eq(EnterpriseEmployee::userId, employeeUserId))?.let { enterpriseEmployee ->

                            personSelfPic = enterpriseEmployee.lifePic?.split(",")?.get(0)
                            jobName = enterpriseEmployee.jobId?.let { iJobService.getJobName(it) }
                        } }

                    }

                    examRoom = iExamRoomService.getById(examRoomId)?.examName
                    supervisionName = userService.get(supervisionUserId)?.nickname
                    jobTrainPartName = iSecurityAdvisorJobTrainPartService.getById(jobTrainPartId)?.trainPartTitle

                iExamResultConfirmCheckService.getOne(KtQueryWrapper(ExamResultConfirmCheck::class.java)
                        .eq(ExamResultConfirmCheck::officialExamRecordId, id))?.let {

                    ToolUtil.copyProperties(it, this)
                }
            }
        }
    }

    override fun getMyEmployeeOfficialExamLast(): EmployeeOfficialExamRecord? {

        return getOne(KtQueryWrapper(EmployeeOfficialExamRecord::class.java)
                .eq(EmployeeOfficialExamRecord::createBy, securityUtil.currUser.id )
                .orderByDesc(EmployeeOfficialExamRecord::createTime)
                .last("limit 1"))
    }


}